package com.ruoyi.system.service.service;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.IdUtil;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ruoyi.system.entity.Orders;
import com.ruoyi.system.mapper.OrdersMapper;
import com.ruoyi.system.mapper.TemplateMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 业务处理
 **/
@Service
public class OrdersService {

    @Resource
    private OrdersMapper ordersMapper;

    @Resource
    private TemplateMapper templateMapper;

    /**
     * 新增
     */
    public void add(Orders orders) {


//        orders.setTransactionId(Integer.valueOf(IdUtil.getSnowflakeNextIdStr()));

        String snowflakeIdStr = IdUtil.getSnowflakeNextIdStr();
        // 将字符串转换为long值
        long snowflakeId = Long.parseLong(snowflakeIdStr);
        // 取低32位，并转换为int值
        int transactionId =Math.abs((int) (snowflakeId & 0xFFFFFFFFL));
        orders.setTransactionId(transactionId);
        orders.setTransactionStatus(5);
        orders.setCreateTime(DateUtil.now());
        orders.setBillingMethod("alipay");

        ordersMapper.insert(orders);
        templateMapper.insertTemplate(orders.getMerchantId());
    }

    /**
     * 删除
     */
    public void deleteById(Integer id) {
        ordersMapper.deleteById(id);
    }

    /**
     * 批量删除
     */
    public void deleteBatch(List<Integer> ids) {
        for (Integer id : ids) {
            ordersMapper.deleteById(id);
        }
    }

    /**
     * 修改
     */
    public void updateById(Orders orders) {
        ordersMapper.updateById(orders);
    }

    /**
     * 根据ID查询
     */
    public Orders selectById(Integer id) {
        return ordersMapper.selectById(id);
    }

    /**
     * 查询所有
     */
    public List<Orders> selectAll(Orders orders) {
        return ordersMapper.selectAll(orders);
    }

    /**
     * 分页查询
     */
    public PageInfo<Orders> selectPage(Orders orders, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<Orders> list = ordersMapper.selectAll(orders);
        return PageInfo.of(list);
    }

    public Orders selectByOrderNo(String orderNo) {
        return ordersMapper.selectByOrderNo(orderNo);
    }

    public void requestRefund(Orders orders) {
        ordersMapper.requestRefund(orders);

    }
}